package com.jqb.webservice.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
@WebService(endpointInterface="com.jqb.webservice.service.IUploadService",
//			wsdlLocation="META-INF/wsdl/UploadService.wsdl",
			wsdlLocation="WEB-INF/wsdl/UploadService.wsdl",
			targetNamespace="http://com.jqb.webservice/upload",
			portName="UploadServicePort",serviceName="UploadService")
/*方式一：@MTOM(表示开启接收附件(测试了下不加也行,客户端使用MTOM就可以,不知道为什么))*/
//@MTOM
/*方式二：@BindingType(设置)*/
@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class UploadService implements IUploadService {

	/*方式一
	@Override
	public void upload(byte[] file) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("D:\\Workroom\\java_room\\workspace_webservice\\wsimport\\webservice11\\aaa.jpg");
			fos.write(file);
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}*/
	
	/*方式二*/
	@Override
	public void upload(DataHandler file) {
		FileOutputStream fos = null;
		InputStream  is = null;
		try {
			is = file.getInputStream();
			fos = new FileOutputStream("test/main/resources/t.jpg");
			System.out.println("contentType:"+file.getContentType());
			int len = 0;
			byte[] buff = new byte[1024];
			while((len=is.read(buff))>0){
				fos.write(buff,0,len);
			}
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
